package player

import (
	"github.com/name5566/leaf/log"
	"go.mongodb.org/mongo-driver/bson"
	"server/db"
)

type PlayerDao struct {
	db *db.MongoMiddleware
}

func NewPlayerDao() *PlayerDao {
	return &PlayerDao{
		db: db.Db,
	}
}
func (p *PlayerDao) updateLastShouRuTime(userId string, time int64) error {
	err := p.db.Update(TBL_USER, bson.M{"uid": userId}, map[string]interface{}{
		"$set": map[string]interface{}{"last_shou_ru_time": time},
	})
	if err != nil {
		log.Error("error msg is %v", err)
		return err
	}
	return nil
}
func (p *PlayerDao) updateCoins(userId string, coins int) error {
	err := p.db.Update(TBL_USER, bson.M{"uid": userId}, map[string]interface{}{
		"$set": map[string]interface{}{"coins": coins},
	})
	if err != nil {
		log.Error("error msg is %v", err)
		return err
	}
	return nil
}
func (p *PlayerDao) updateXuanChuanTiLi(userId string, chuanTiLI int) error {
	err := p.db.Update(TBL_USER, bson.M{"uid": userId}, map[string]interface{}{
		"$set": map[string]interface{}{"xuan_chuan_ti_li": chuanTiLI},
	})
	if err != nil {
		log.Error("error msg is %v", err)
		return err
	}
	return nil
}
func (p *PlayerDao) joinUnion(userId string, unionId int32) error {
	err := p.db.Update(TBL_USER, bson.M{"uid": userId}, map[string]interface{}{
		"$set": map[string]interface{}{"union_id": unionId},
	})
	if err != nil {
		log.Error("error msg is %v", err)
		return err
	}
	return nil
}
func (p *PlayerDao) updateUserShouChong(userId string, shouChong []ShouChong) error {
	err := p.db.Update(TBL_USER, bson.M{"uid": userId}, map[string]interface{}{
		"$set": map[string]interface{}{"shou_chong_info": shouChong},
	})
	if err != nil {
		log.Error("error msg is %v", err)
		return err
	}
	return nil
}
func (p *PlayerDao) getPlayerListFromDatabase(arrUid []string) ([]Player, error) {
	filter := bson.M{"uid": bson.M{"$in": arrUid}}
	var results []Player
	err := p.db.Find(TBL_USER, filter, &results)
	return results, err
}

// 模拟从数据库获取玩家信息的函数
func (p *PlayerDao) getPlayerFromDatabase(userId string, key string) (Player, error) {
	// 这里可以根据 UID 从数据库查询玩家信息，例如使用 MongoDB 或 MySQL
	// 以下是一个简单的示例，返回一个空的 Player 结构体
	var player Player
	err := p.db.FindOne(TBL_USER, bson.M{key: userId}, &player)
	return player, err
}
func (p *PlayerDao) insert(player Player) (Player, error) {
	err := p.db.Insert(TBL_USER, player)
	return player, err
}
